package com.yaolisheng.controller;


import com.yaolisheng.pojo.SysConfig;
import com.yaolisheng.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SysConfig)表控制层
 *
 * @author yaolisheng
 * @since 2021-05-04 00:51:24
 */
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {
    /**
     * 服务对象
     */
    @Resource
    private SysConfigService sysConfigService;

    @Resource
    private DiscoveryClient client;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public SysConfig selectOne(String id) {
        return this.sysConfigService.queryById(id);
    }

    @RequestMapping("/get")
    public List<SysConfig> queryAll() {
        List<SysConfig> sysConfigs = this.sysConfigService.queryAll(new SysConfig());
        System.out.println(sysConfigs);
        return sysConfigs;
    }

    @RequestMapping
    public Map<String, Boolean> insert(SysConfig sysConfig) {
        Map map = new HashMap<String, Boolean>();
        SysConfig insert = this.sysConfigService.insert(sysConfig);
        if (insert != null) {
            map.put("success", true);
        }
        return map;
    }

    //注册进来的微服务~获取一些消息
    @RequestMapping("/dis")
    public Object discovery() {
//        获取服务器列表清单
        List<String> services = client.getServices();
        System.out.println("discovery===>" + services);
        List<ServiceInstance> instances = client.getInstances("PROVIDER-YLS");
        for (ServiceInstance ser : instances) {
            System.out.println(ser.getUri());
            System.out.println(ser.getPort());
            System.out.println(ser.getServiceId());
            System.out.println(ser.getHost());
        }
        return instances;
    }


}